<?php
/**
 * Created by PhpStorm.
 * User: zhao
 * Date: 2018/5/31
 * Time: 15:53
 */

namespace App\Obersvers;


use App\Models\BalanceRecord;
use App\Models\ContinueSigninRecord;
use App\Models\CreditsRecod;
use App\Models\MerchantTicket;

class ContinueSigninRecordObersver
{
    public function created(ContinueSigninRecord $record)
    {

//        $insert_data['id_merchant'] = $record->id_merchant;
        $insert_data['id_wxuser_mcard'] = $record->id_wxuser_mcard;
        $insert_data['id_merchant'] = $record->id_merchant;
        $insert_data['table_type'] = 'continue_signin_record';
        $insert_data['table_id'] = $record->id;
        $insert_data['title'] = '连续签到奖励';
        $insert_data['desc'] = '连续签到领取的大礼包';

        $time = now()->toDateTimeString();
        $insert_data['created_at'] = $time;
        $insert_data['updated_at'] = $time;

        if($record->credits){
            $insert_data['value'] = $record->credits;
            CreditsRecod::create($insert_data);
        }

        if($record->cash){
            $insert_data['value'] = $record->cash;

            BalanceRecord::create($insert_data);
        }

        if($record->id_tickets){
            $tickets = MerchantTicket::whereIn('id_merchant_ticket',explode(',',$record->id_tickets))->where('is_delete','N')->get();
            $data = [];
            foreach ($tickets as $ticket){
                $data[] =  [
                    'id_wxuser_mcard'=>$record->id_wxuser_mcard,
                    'id_merchant_ticket'=>$ticket->id_merchant_ticket,
                    'id_merchant'=>$record->id_merchant,
                    'desc'=>'连续签到奖励',
                    'status'=>1,
                    'table_type'=>'credits_exchange_record',
                    'table_id'=>$record->id,
                    'proof_expiry_time'=>now()->addDays($ticket->expiry_days)->toDateTimeString(),
                    'created_at'=>$time,
                    'updated_at'=>$time,
                ];
            }

            if(!empty($data)) \DB::table('wxcard_tickets')->insert($data);


        }
    }
}